<?php

require_once("functions.php");
connectToDb();

$playgroundName = $_REQUEST['playgroundName'];
$page = $_REQUEST['page'];
$template = $_REQUEST['template'];
$publicToken = $_REQUEST['publicToken'];
$privateToken = $_REQUEST['privateToken'];

$pageOffset = ($page -1) * 5;

$query = "SELECT id, name, content, is_template, is_featured, user_id, description, author, public_token, private_token, image, thumbnail_image, created_date, edited_date 
	FROM design 
	WHERE (1=1) ";
if(! is_null($playgroundName)) {
	$query = $query . "AND (name = '$playgroundName') ";
}
if(! is_null($template)) {
	$query = $query . "AND (is_template = '$template') ";
}
if(! is_null($publicToken)) {
	$query = $query . "AND (public_token = '$publicToken') ";
}
if(! is_null($privateToken)) {
	$query = $query . "AND (private_token = '$privateToken') ";
}
$query = $query . "ORDER BY created_date DESC ";
if(! is_null($page)) {
	$query = $query . "LIMIT 5 OFFSET $pageOffset";
}

$result = mysql_query($query);

$output = array();
while ($row = mysql_fetch_assoc($result)) {
    $output[] = array(
        "id" => $row["id"],
        "name" => $row["name"],
        "content" => json_decode($row["content"], true),
		"is_template" => $row["is_template"],
		"is_featured" => $row["is_featured"],
		"user_id" => $row["user_id"],
		"description" => $row["description"],
		"author" => $row["author"],
		"public_token" => $row["public_token"],
		"image" => $row["image"],
		"thumbnail_image" => $row["thumbnail_image"],
		"created_date" => $row["created_date"],
		"edited_date" => $row["edited_date"]
    );
}

echo json_encode($output);
